home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / MacSource / TE32K / main.c next >
Encoding:
C/C++ Source or Header  |  1994-07-21  |  24.3 KB  |  1,191 lines  |  [TEXT/KAHL]

  1. #include <Desk.h>
  2. #include <Errors.h>
  3. #include <Fonts.h>
  4. #include <Memory.h>
  5. #include <Menus.h>
  6. #include <StandardFile.h>
  7. #include <Strings.h>
  8. #include <ToolUtils.h>
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "TE32K.h"
  13.  
  14.  
  15. #define EQSTR    0
  16.  
  17.  
  18. #define    yesNoCancelDLOG    132
  19. #define    errorDLOG        256
  20.  
  21. #define    numMenus    4
  22.  
  23. #define    appleMenu    0
  24. #define    fileMenu    1
  25. #define    editMenu    2
  26. #define fontMenu    3
  27.  
  28. #define    appleID        128
  29. #define    fileID        129
  30. #define    editID        130
  31. #define fontID        131
  32.  
  33. #define    newCommand        1
  34. #define    openCommand        2
  35. #define insertCommand    3
  36. #define    closeCommand    4
  37. #define    saveCommand        5
  38. #define    saveAsCommand    6
  39. #define makeBigCommand    8
  40. #define    quitCommand        10
  41.  
  42. #define    undoCommand        1
  43. #define    cutCommand        3
  44. #define    copyCommand        4
  45. #define pasteCommand    5
  46. #define    clearCommand    6
  47. #define    selectCommand    7
  48. #define    wrapCommand        9
  49.  
  50.  
  51.  
  52. char            alive;
  53. MenuHandle        myMenus[numMenus];
  54. char            tempString[256];
  55. WindowPtr        theWPtr = 0L;
  56. TE32KHandle        theTEH32K = 0L;
  57. ControlHandle    theVScroll = 0L, theHScroll = 0L;
  58. Cursor            editCursor,waitCursor;
  59. SFReply            mySFReply;
  60. char            changed;
  61. short                defaultFont,defaultFontSize;
  62. char            *theTestString = "This is terminated by a \\n character\n";
  63. UserItemUPP        defaultItemUPP ;
  64.  
  65. #if defined(powerc) || defined(__powerc)
  66. QDGlobals            qd;
  67. #endif
  68.  
  69. RestartProc()
  70. {
  71.     ExitToShell();
  72. }
  73.  
  74.  
  75.  
  76. main()
  77. {
  78. EventRecord        theEvent;
  79. unsigned char    theChar;
  80.  
  81.     InitGraf(&qd.thePort);
  82.     InitFonts();
  83.     FlushEvents(everyEvent,0);
  84.     InitWindows();
  85.     InitMenus();
  86.     TEInit();
  87.     InitDialogs(0);
  88.     InitCursor();
  89.     
  90.     TE32KInit();
  91.     
  92.     SetUpCursors();
  93.     
  94.     SetUpMenus();
  95.     
  96.     defaultFont = monaco;
  97.     defaultFontSize = 9;
  98.     MashortainFontMenu();
  99.     
  100.     FlushEvents(everyEvent,0);
  101.     
  102.     alive = true;
  103.     
  104.     while (alive)
  105.     {
  106.         SystemTask();
  107.         MashortainCursor();
  108.         MashortainMenus();
  109.         DoIdle();
  110.         
  111.         if (GetNextEvent(everyEvent,&theEvent))
  112.         {
  113.             switch (theEvent.what)
  114.             {
  115.                 case mouseDown:
  116.                     DoMouseDown(&theEvent);
  117.                     break;
  118.                 
  119.                 case keyDown:
  120.                 case autoKey: 
  121.                     
  122.                     theChar = theEvent.message & charCodeMask;
  123.                     
  124.                     if (theEvent.modifiers & cmdKey)
  125.                         DoCommand(MenuKey((unsigned char) theChar));
  126.                     
  127.                     else
  128.                         DoKey((unsigned char) theChar);
  129.                     
  130.                     break;
  131.                     
  132.                 case activateEvt:
  133.                     if (IsOurWindow((WindowPtr) theEvent.message)) 
  134.                         DoActivateDeactivate((WindowPtr) theEvent.message,(char) (theEvent.modifiers & activeFlag));
  135.                     
  136.                     break;
  137.                     
  138.                 case updateEvt: 
  139.                     if (IsOurWindow((WindowPtr) theEvent.message)) 
  140.                         UpdateWindow((WindowPtr) theEvent.message);
  141.                     
  142.                     break;
  143.             }
  144.         }
  145.     }
  146. }
  147.  
  148.  
  149.  
  150.  
  151.  
  152. pascal void HiliteDefaultButton(theDPtr, whichItem)
  153. DialogPtr theDPtr; short whichItem;
  154. {
  155. short            type;
  156. Handle        theItem;
  157. Rect        theRect;
  158.  
  159.     GetDItem(theDPtr, ((DialogPeek) theDPtr)->aDefItem, &type, &theItem, &theRect);
  160.     PenNormal();
  161.     PenSize(3,3);
  162.     InsetRect(&theRect,-4,-4);
  163.     FrameRoundRect(&theRect,16,16);
  164.     PenSize(1,1);
  165. }
  166.  
  167.  
  168.  
  169. ErrorAlert(p0)
  170. char    *p0;
  171. {
  172. GrafPtr            oldPort;
  173. DialogPtr        theDPtr,tempDPtr;
  174. short                itemHit, type;
  175. Handle            theItem;
  176. Rect            theRect;
  177. EventRecord        theEvent;
  178.  
  179.     GetPort(&oldPort);
  180.     
  181.     InitCursor();
  182.     
  183.     if (!(theDPtr = GetNewDialog(errorDLOG, NULL,(WindowPtr) -1L)))
  184.     {
  185.         SysBeep(1);
  186.         ExitToShell();
  187.     }
  188.     
  189.     SetPort(theDPtr);
  190.     
  191.     CenterWindow(theDPtr);
  192.     ShowWindow(theDPtr);
  193.     
  194.     ((DialogPeek) theDPtr)->aDefItem = 1;
  195.     
  196.     if (!defaultItemUPP)
  197.         defaultItemUPP = NewUserItemProc(HiliteDefaultButton);
  198.     GetDItem(theDPtr, 3, &type, &theItem, &theRect);
  199.     SetDItem(theDPtr, 3, type, (Handle) defaultItemUPP, &theRect);
  200.     
  201.     GetDItem(theDPtr, 2, &type, &theItem, &theRect);
  202.     C2PStr(p0);
  203.     SetIText(theItem, (ConstStr255Param) p0);
  204.     P2CStr((unsigned char *) p0);
  205.     
  206.     while (!GetNextEvent(updateMask,&theEvent));
  207.     
  208.     if (theEvent.message == (long) theDPtr)
  209.         DialogSelect(&theEvent,&tempDPtr,&itemHit);
  210.     
  211.     SysBeep(1);
  212.     
  213.     do 
  214.     {
  215.         itemHit = 0;
  216.         
  217.         while (!GetNextEvent(everyEvent,&theEvent));
  218.         
  219.         if (theEvent.what==keyDown || theEvent.what==autoKey)
  220.         {
  221.             if ((theEvent.message & charCodeMask)=='\r' || (theEvent.message & charCodeMask)==0x03)
  222.                 itemHit = ((DialogPeek) theDPtr)->aDefItem;
  223.             else
  224.                 SysBeep(1);
  225.         }
  226.         
  227.         else
  228.         {
  229.             tempDPtr = (DialogPtr) 0L;
  230.             
  231.             if (!IsDialogEvent(&theEvent) || !DialogSelect(&theEvent,&tempDPtr,&itemHit) || tempDPtr!=theDPtr)
  232.                 itemHit = 0;
  233.         }
  234.         
  235.     } while (itemHit!=1);
  236.     
  237.     DisposDialog(theDPtr);
  238.     SetPort(oldPort);
  239. }
  240.  
  241.  
  242.  
  243.  
  244.  
  245. SetUpMenus()
  246. {
  247. short            i,numDAs;
  248.  
  249.     myMenus[appleMenu] = GetMenu(appleID);
  250.     AddResMenu(myMenus[appleMenu],'DRVR');
  251.     myMenus[fileMenu] = GetMenu(fileID);
  252.     myMenus[editMenu] = GetMenu(editID);
  253.     myMenus[fontMenu] = GetMenu(fontID);
  254.     InsertResMenu(myMenus[fontMenu],'FONT',0);
  255.     
  256.     InsertMenu(myMenus[appleMenu],0);
  257.     InsertMenu(myMenus[fileMenu],0);
  258.     InsertMenu(myMenus[editMenu],0);
  259.     InsertMenu(myMenus[fontMenu],0);
  260.     
  261.     DrawMenuBar();
  262.     
  263.     numDAs = CountMItems(myMenus[appleMenu]);
  264.     
  265.     for (i=1;i<=numDAs;i++)
  266.         EnableItem(myMenus[appleMenu],i);
  267. }
  268.  
  269.  
  270.  
  271. MashortainMenus()
  272. {
  273. short        i,numItems;
  274.  
  275.     if (theWPtr && FrontWindow() == theWPtr)
  276.     {
  277.         DisableItem(myMenus[fileMenu],newCommand);
  278.         DisableItem(myMenus[fileMenu],openCommand);
  279.         EnableItem(myMenus[fileMenu],insertCommand);
  280.         EnableItem(myMenus[fileMenu],closeCommand);
  281.         
  282.         if (changed)
  283.             EnableItem(myMenus[fileMenu],saveCommand);
  284.         else
  285.             DisableItem(myMenus[fileMenu],saveCommand);
  286.         
  287.         EnableItem(myMenus[fileMenu],saveAsCommand);
  288.         
  289.         
  290.         DisableItem(myMenus[editMenu],undoCommand);
  291.         
  292.         if (theTEH32K)
  293.         {
  294.             if ((**theTEH32K).selStart == (**theTEH32K).selEnd)
  295.             {
  296.                 DisableItem(myMenus[editMenu],cutCommand);
  297.                 DisableItem(myMenus[editMenu],copyCommand);
  298.                 DisableItem(myMenus[editMenu],clearCommand);
  299.             }
  300.             
  301.             else
  302.             {
  303.                 EnableItem(myMenus[editMenu],cutCommand);
  304.                 EnableItem(myMenus[editMenu],copyCommand);
  305.                 EnableItem(myMenus[editMenu],clearCommand);
  306.             }
  307.             
  308.             if (TE32KGetScrapLen() > 0L)
  309.                 EnableItem(myMenus[editMenu],pasteCommand);
  310.             else
  311.                 DisableItem(myMenus[editMenu],pasteCommand);
  312.             
  313.             EnableItem(myMenus[editMenu],selectCommand);
  314.             EnableItem(myMenus[editMenu],wrapCommand);
  315.             
  316.             if (theTEH32K && !(**theTEH32K).crOnly)
  317.             {
  318.                 CheckItem(myMenus[editMenu],wrapCommand,true);
  319.                 SetItemMark(myMenus[editMenu],wrapCommand,checkMark);
  320.             }
  321.                 
  322.             else
  323.                 CheckItem(myMenus[editMenu],wrapCommand,false);
  324.         }
  325.         
  326.         
  327.         numItems = CountMItems(myMenus[fontMenu]);
  328.         
  329.         for (i=1;i<=numItems;i++)
  330.             EnableItem(myMenus[fontMenu],i);
  331.         
  332.         MashortainFontMenu();
  333.     }
  334.     
  335.     else
  336.     {
  337.         EnableItem(myMenus[fileMenu],newCommand);
  338.         EnableItem(myMenus[fileMenu],openCommand);
  339.         DisableItem(myMenus[fileMenu],insertCommand);
  340.         DisableItem(myMenus[fileMenu],closeCommand);
  341.         DisableItem(myMenus[fileMenu],saveCommand);
  342.         DisableItem(myMenus[fileMenu],saveAsCommand);
  343.         
  344.         
  345.         EnableItem(myMenus[editMenu],undoCommand);
  346.         EnableItem(myMenus[editMenu],cutCommand);
  347.         EnableItem(myMenus[editMenu],copyCommand);
  348.         EnableItem(myMenus[editMenu],pasteCommand);
  349.         EnableItem(myMenus[editMenu],clearCommand);
  350.         DisableItem(myMenus[editMenu],selectCommand);
  351.         DisableItem(myMenus[editMenu],wrapCommand);
  352.         CheckItem(myMenus[editMenu],wrapCommand,false);
  353.         
  354.         numItems = CountMItems(myMenus[fontMenu]);
  355.         
  356.         for (i=1;i<=numItems;i++)
  357.         {
  358.             DisableItem(myMenus[fontMenu],i);
  359.             CheckItem(myMenus[fontMenu],i,false);
  360.         }
  361.     }
  362.  
  363. }
  364.  
  365.  
  366. DoCommand(mResult)
  367. long mResult;
  368. {
  369. short            theItem,numItems;
  370. GrafPtr        oldPort;
  371. SFReply        oldSFReply;
  372. Rect        tempRect;
  373.  
  374.  
  375.     theItem = LoWord(mResult);
  376.     
  377.     switch (HiWord(mResult)) 
  378.     {
  379.         case appleID:
  380.             if (theItem == 1)
  381.             {
  382.                 DoAboutBox(0);
  383.             }
  384.             
  385.             else
  386.             {
  387.                 GetPort(&oldPort);
  388.                 GetItem(myMenus[appleMenu], theItem, (unsigned char *) tempString);
  389.                 OpenDeskAcc((unsigned char *) tempString);
  390.                 SetPort(oldPort);
  391.             }
  392.             
  393.             break;
  394.             
  395.         case fileID:
  396.             switch (theItem)
  397.             {
  398.                 case newCommand:
  399.                     if (!theTEH32K)
  400.                         DoShowWindow();
  401.                     else
  402.                         SysBeep(1);
  403.                     
  404.                     break;
  405.                 
  406.                 case openCommand:
  407.                     if (theTEH32K && DoCloseWindow(theWPtr))
  408.                     {
  409.                         DoShowWindow();
  410.                         
  411.                         if (OpenTextFile(theTEH32K,&mySFReply))
  412.                         {
  413.                             SetWTitle(theWPtr,mySFReply.fName);
  414.                             AdjustScrollBar();
  415.                             changed = false;
  416.                         }
  417.                     }
  418.                     
  419.                     else if (!theTEH32K)
  420.                     {
  421.                         DoShowWindow();
  422.                         
  423.                         if (OpenTextFile(theTEH32K,&mySFReply))
  424.                         {
  425.                             SetWTitle(theWPtr,mySFReply.fName);
  426.                             AdjustScrollBar();
  427.                             changed = false;
  428.                         }
  429.                     }
  430.                     
  431.                     break;
  432.                 
  433.                 case insertCommand:
  434.                     if (theTEH32K)
  435.                     {
  436.                         changed = InsertTextFile(theTEH32K);
  437.                         
  438.                         AdjustScrollBar();
  439.                     }
  440.                     
  441.                     break;
  442.                     
  443.                 case closeCommand:
  444.                     if (theTEH32K)
  445.                         DoCloseWindow(theWPtr);
  446.                     else
  447.                         SysBeep(1);
  448.                         
  449.                     break;
  450.                 
  451.                 case saveCommand:
  452.                     if (theTEH32K)
  453.                     {
  454.                         if (SaveTextFile(theTEH32K,&mySFReply))
  455.                         {
  456.                             SetWTitle(theWPtr,mySFReply.fName);
  457.                             changed = false;
  458.                         }
  459.                     }
  460.                         
  461.                     else
  462.                         SysBeep(1);
  463.                     
  464.                     break;
  465.                 
  466.                 case saveAsCommand:
  467.                     if (theTEH32K)
  468.                     {
  469.                         oldSFReply = mySFReply;
  470.                         mySFReply.good = false;
  471.                         
  472.                         if (SaveTextFile(theTEH32K,&mySFReply))
  473.                         {
  474.                             changed = false;
  475.                             SetWTitle(theWPtr,mySFReply.fName);
  476.                         }
  477.                         else
  478.                             mySFReply = oldSFReply;
  479.                     }
  480.                         
  481.                     else
  482.                         SysBeep(1);
  483.                     
  484.                     break;
  485.                 
  486.                 case makeBigCommand:
  487.                     CreateBigFile();
  488.                     break;
  489.                 
  490.                 case quitCommand:
  491.                     if (theTEH32K)
  492.                     {
  493.                         if (DoCloseWindow(theWPtr))
  494.                             alive = false;
  495.                     }
  496.                     else
  497.                         alive = false;
  498.                     
  499.                     break;
  500.             }
  501.             
  502.             break;
  503.         
  504.         case editID:
  505.             switch(theItem)
  506.             {
  507.                 case cutCommand:
  508.                     if (!DoCut())
  509.                         SystemEdit(theItem - 1);
  510.                     break;
  511.                 
  512.                 case copyCommand:
  513.                     if (!DoCopy())
  514.                         SystemEdit(theItem - 1);
  515.                     break;
  516.                 
  517.                 case pasteCommand:
  518.                     if (!DoPaste())
  519.                         SystemEdit(theItem - 1);
  520.                     
  521.                     break;
  522.                 
  523.                 case clearCommand:
  524.                     if (!DoClear())
  525.                         SystemEdit(theItem - 1);
  526.                     
  527.                     break;
  528.                 
  529.                 case selectCommand:
  530.                     if (!DoSelectAll())
  531.                         SystemEdit(theItem - 1);
  532.                     
  533.                     break;
  534.                 
  535.                 case wrapCommand:
  536.                     if (theWPtr && theWPtr == FrontWindow() && theTEH32K)
  537.                     {
  538.                         (**theTEH32K).crOnly = !(**theTEH32K).crOnly;
  539.                         
  540.                         TE32KCalText(theTEH32K);
  541.                         
  542.                         tempRect = theWPtr->portRect;
  543.                         GetPort(&oldPort);
  544.                         SetPort(theWPtr);
  545.                         InvalRect(&tempRect);
  546.                         SetPort(oldPort);
  547.                         
  548.                         AdjustForResizedWindow();
  549.                     }
  550.                     
  551.                     break;
  552.                 
  553.                 
  554.                 default:
  555.                     SystemEdit(theItem - 1);
  556.                     break;
  557.             }
  558.             
  559.             break;
  560.             
  561.         case fontID:
  562.             numItems = CountMItems(myMenus[fontMenu]);
  563.             
  564.             if (theItem <= numItems-7)
  565.             {
  566.                 GetItem(myMenus[fontMenu], theItem, (unsigned char *) tempString);
  567.                 P2CStr((unsigned char *) tempString);
  568.                 DoFontSize(tempString,-1);
  569.             }
  570.             
  571.             else
  572.             {
  573.                 GetItem(myMenus[fontMenu], theItem, (unsigned char *) tempString);
  574.                 P2CStr((unsigned char *) tempString);
  575.                 
  576.                 if (sscanf(tempString,"%d",&numItems)==1)
  577.                 {
  578.                     tempString[0] = 0;
  579.                     DoFontSize(tempString,numItems);
  580.                 }
  581.             }
  582.             
  583.             break;
  584.     }
  585.     
  586.     HiliteMenu(0);
  587. }
  588.  
  589.  
  590.  
  591.  
  592. OpenTextFile(theTEH,mySFReply)
  593. TE32KHandle    theTEH;SFReply    *mySFReply;
  594. {
  595. SFTypeList    mySFTypes;
  596. Point        theTopLeft;
  597. short            errCode,fRefNum;
  598. long        byteCount;
  599. Ptr            theText;
  600.  
  601.  
  602.     if (theTEH)
  603.     {
  604.         theTopLeft.h = qd.screenBits.bounds.left + (qd.screenBits.bounds.right - qd.screenBits.bounds.left - 348)/2;;
  605.         theTopLeft.v = qd.screenBits.bounds.top + 50;
  606.         
  607.         mySFTypes[0] = 'TEXT';
  608.         
  609.         InitCursor();
  610.         
  611.         SFGetFile(theTopLeft,"\pSelect Text File To Open:",0L,1,mySFTypes,0L,mySFReply);
  612.         
  613.         if (mySFReply->good)
  614.         {
  615.             if ((errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum))!=noErr)
  616.             {
  617.                 mySFReply->good = false;
  618.                 P2CStr((unsigned char *) mySFReply->fName);
  619.                 sprintf(tempString,"OpenTextFile: FSOpen('%s') Error %d",mySFReply->fName,errCode);
  620.                 ErrorAlert(tempString);
  621.                 return(false);
  622.             }
  623.             
  624.             if ((errCode = GetEOF(fRefNum,&byteCount))!=noErr)
  625.             {
  626.                 mySFReply->good = false;
  627.                 FSClose(fRefNum);
  628.                 P2CStr((unsigned char *) mySFReply->fName);
  629.                 sprintf(tempString,"OpenTextFile: GetEOF('%s',0) Error %d",mySFReply->fName,errCode);
  630.                 ErrorAlert(tempString);
  631.                 return(false);
  632.             }
  633.             
  634.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  635.             {
  636.                 mySFReply->good = false;
  637.                 FSClose(fRefNum);
  638.                 P2CStr((unsigned char *) mySFReply->fName);
  639.                 sprintf(tempString,"OpenTextFile: SetFPos('%s',0) Error %d",mySFReply->fName,errCode);
  640.                 ErrorAlert(tempString);
  641.                 return(false);
  642.             }
  643.             
  644.             theText = NewPtr(byteCount);
  645.             
  646.             if (MemError())
  647.             {
  648.                 mySFReply->good = false;
  649.                 FSClose(fRefNum);
  650.                 sprintf(tempString,"OpenTextFile: NewPtr(%ld) Error %d",byteCount,MemError());
  651.                 ErrorAlert(tempString);
  652.                 return(false);
  653.             }
  654.             
  655.             SetCursor(&waitCursor);
  656.             
  657.             if ((errCode = FSRead(fRefNum,&byteCount,theText))!=noErr)
  658.             {
  659.                 InitCursor();
  660.                 mySFReply->good = false;
  661.                 DisposePtr(theText);
  662.                 FSClose(fRefNum);
  663.                 P2CStr((unsigned char *) mySFReply->fName);
  664.                 sprintf(tempString,"OpenTextFile: FSRead('%s') Error %d",mySFReply->fName,errCode);
  665.                 ErrorAlert(tempString);
  666.                 return(false);
  667.             }
  668.             
  669.             if ((errCode = FSClose(fRefNum))!=noErr)
  670.             {
  671.                 InitCursor();
  672.                 mySFReply->good = false;
  673.                 DisposePtr(theText);
  674.                 P2CStr((unsigned char *) mySFReply->fName);
  675.                 sprintf(tempString,"OpenTextFile: FSClose('%s',0) Error %d",mySFReply->fName,errCode);
  676.                 ErrorAlert(tempString);
  677.                 return(false);
  678.             }
  679.             
  680.             TE32KSetText(theText,byteCount,theTEH);
  681.             
  682.             DisposePtr(theText);
  683.             
  684.             InitCursor();
  685.             
  686.             return(true);
  687.         }
  688.         
  689.         else
  690.             return(false);
  691.     }
  692.     
  693.     else
  694.         return(false);
  695. }
  696.  
  697.  
  698.  
  699. SaveTextFile(theTEH,mySFReply)
  700. TE32KHandle    theTEH;SFReply    *mySFReply;
  701. {
  702. Point        theTopLeft;
  703. short            errCode,fRefNum;
  704. long        byteCount;
  705. Handle        theText;
  706.  
  707.     if (theTEH)
  708.     {
  709.         theTopLeft.h = qd.screenBits.bounds.left + (qd.screenBits.bounds.right - qd.screenBits.bounds.left - 304)/2;;
  710.         theTopLeft.v = qd.screenBits.bounds.top + 50;
  711.         
  712.         InitCursor();
  713.         
  714.         if (!(mySFReply->good))
  715.             SFPutFile(theTopLeft,"\pSave Text File As:","\p",0L,mySFReply);
  716.         
  717.         if (mySFReply->good)
  718.         {
  719.             if ((errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum))!=noErr)
  720.             {
  721.                 if (errCode==fnfErr)
  722.                 {
  723.                     if ((errCode = Create(mySFReply->fName,mySFReply->vRefNum,
  724.                                             '    ','TEXT')) != noErr)
  725.                     {
  726.                         mySFReply->good = false;
  727.                         P2CStr((unsigned char *) mySFReply->fName);
  728.                         sprintf(tempString,"SaveTextFile: Create('%s') Error %d",mySFReply->fName,errCode);
  729.                         ErrorAlert(tempString);
  730.                         return(false);
  731.                     }
  732.                     
  733.                     errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum);
  734.                 }
  735.                 
  736.                 if (errCode != noErr)
  737.                 {
  738.                     mySFReply->good = false;
  739.                     P2CStr((unsigned char *) mySFReply->fName);
  740.                     sprintf(tempString,"SaveTextFile: FSOpen('%s') Error %d",mySFReply->fName,errCode);
  741.                     ErrorAlert(tempString);
  742.                     return(false);
  743.                 }
  744.             }
  745.             
  746.             if ((errCode = SetEOF(fRefNum,0L))!=noErr)
  747.             {
  748.                 mySFReply->good = false;
  749.                 FSClose(fRefNum);
  750.                 P2CStr((unsigned char *) mySFReply->fName);
  751.                 sprintf(tempString,"SaveTextFile: SetEOF('%s',0) Error %d",mySFReply->fName,errCode);
  752.                 ErrorAlert(tempString);
  753.                 return(false);
  754.             }
  755.             
  756.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  757.             {
  758.                 mySFReply->good = false;
  759.                 FSClose(fRefNum);
  760.                 P2CStr((unsigned char *) mySFReply->fName);
  761.                 sprintf(tempString,"SaveTextFile: SetFPos('%s',0) Error %d",mySFReply->fName,errCode);
  762.                 ErrorAlert(tempString);
  763.                 return(false);
  764.             }
  765.             
  766.             theText = (**theTEH).hText;
  767.             byteCount = (**theTEH).teLength;
  768.             
  769.             HLock(theText);
  770.             
  771.             SetCursor(&waitCursor);
  772.             
  773.             if ((errCode = FSWrite(fRefNum,&byteCount,*theText))!=noErr)
  774.             {
  775.                 InitCursor();
  776.                 mySFReply->good = false;
  777.                 HUnlock(theText);
  778.                 FSClose(fRefNum);
  779.                 P2CStr((unsigned char *) mySFReply->fName);
  780.                 sprintf(tempString,"SaveTextFile: FSWrite('%s') Error %d",mySFReply->fName,errCode);
  781.                 ErrorAlert(tempString);
  782.                 return(false);
  783.             }
  784.             
  785.             HUnlock(theText);
  786.             
  787.             if ((errCode = FSClose(fRefNum))!=noErr)
  788.             {
  789.                 InitCursor();
  790.                 mySFReply->good = false;
  791.                 P2CStr((unsigned char *) mySFReply->fName);
  792.                 sprintf(tempString,"SaveTextFile: FSClose('%s',0) Error %d",mySFReply->fName,errCode);
  793.                 ErrorAlert(tempString);
  794.                 return(false);
  795.             }
  796.             
  797.             FlushVol("\p",mySFReply->vRefNum);
  798.             
  799.             InitCursor();
  800.             
  801.             return(true);
  802.         }
  803.         
  804.         else
  805.             return(false);
  806.     }
  807.     
  808.     else
  809.         return(false);
  810. }
  811.  
  812.  
  813.  
  814.  
  815. YesNoCancel(thePrompt,theDefault)
  816. char    *thePrompt;short    theDefault;
  817. {
  818. DialogPtr        theDPtr,tempDPtr;
  819. short                itemHit, type;
  820. Handle            theItem;
  821. Rect            tempRect;
  822. GrafPtr            oldPort;
  823. char            tempString[256];
  824. EventRecord        theEvent;
  825.  
  826.     GetPort(&oldPort);
  827.     
  828.     InitCursor();
  829.     
  830.     theDPtr = GetNewDialog(yesNoCancelDLOG, 0L,(WindowPtr) -1L);
  831.     
  832.     CenterWindow(theDPtr);
  833.     ShowWindow(theDPtr);
  834.     
  835.     SetPort(theDPtr);
  836.     
  837.     ((DialogPeek) theDPtr)->aDefItem = theDefault;
  838.     
  839.     if (!defaultItemUPP)
  840.         defaultItemUPP = NewUserItemProc(HiliteDefaultButton);
  841.     GetDItem(theDPtr, 4, &type, &theItem, &tempRect);
  842.     SetDItem(theDPtr, 4, type, (Handle) defaultItemUPP, &tempRect);
  843.     
  844.     strcpy(tempString,thePrompt);
  845.     C2PStr(tempString);
  846.     GetDItem(theDPtr, 5, &type, &theItem, &tempRect);
  847.     SetIText(theItem, (ConstStr255Param) tempString);
  848.     
  849.     if (GetNextEvent(updateMask,&theEvent) && theEvent.message == (long) theDPtr)
  850.         DialogSelect(&theEvent,&tempDPtr,&itemHit);
  851.     
  852.     SysBeep(1);
  853.     
  854.     do 
  855.     {
  856.         ModalDialog(0L,&itemHit);
  857.         
  858.     } while (!(itemHit>=1 && itemHit<=3));
  859.     
  860.     DisposDialog(theDPtr);
  861.     SetPort(oldPort);
  862.     
  863.     return(itemHit);
  864. }
  865.  
  866.  
  867.  
  868.  
  869. CenterWindow(wPtr)
  870. WindowPtr    wPtr;
  871. {
  872. short        screenWidth,screenHeight,windowWidth,windowHeight,left,top;
  873.  
  874.     if (wPtr == 0L)
  875.         return;
  876.     
  877.     screenWidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
  878.     screenHeight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - 20;
  879.     windowWidth = wPtr->portRect.right - wPtr->portRect.left;
  880.     windowHeight = wPtr->portRect.bottom - wPtr->portRect.top;
  881.     
  882.     left = qd.screenBits.bounds.left + (screenWidth - windowWidth)/2;
  883.     top = qd.screenBits.bounds.top + 20 + (screenHeight - windowHeight)/2;
  884.     
  885.     if (left < 0)
  886.         left = 0;
  887.     
  888.     if (top < 20)
  889.         top = 20;
  890.     
  891.     MoveWindow(wPtr,left,top,false);
  892. }
  893.  
  894.  
  895.  
  896.  
  897. InsertTextFile(theTEH)
  898. TE32KHandle    theTEH;
  899. {
  900. SFReply        mySFReply;
  901. SFTypeList    mySFTypes;
  902. Point        theTopLeft;
  903. short            errCode,fRefNum;
  904. long        byteCount;
  905. Ptr            theText;
  906. char        tempString[256];
  907.  
  908.     if (theTEH)
  909.     {
  910.         theTopLeft.h = qd.screenBits.bounds.left + (qd.screenBits.bounds.right - qd.screenBits.bounds.left - 348)/2;;
  911.         theTopLeft.v = qd.screenBits.bounds.top + 50;
  912.         
  913.         mySFTypes[0] = 'TEXT';
  914.         
  915.         InitCursor();
  916.         
  917.         SFGetFile(theTopLeft,"\pSelect Text File To Insert:",0L,1,mySFTypes,0L,&mySFReply);
  918.         
  919.         if (mySFReply.good)
  920.         {
  921.             if ((errCode = FSOpen(mySFReply.fName,mySFReply.vRefNum,&fRefNum))!=noErr)
  922.             {
  923.                 P2CStr((unsigned char *) mySFReply.fName);
  924.                 sprintf(tempString,"InsertTextFile: FSOpen('%s') Error %d",mySFReply.fName,errCode);
  925.                 ErrorAlert(tempString);
  926.                 return(true);
  927.             }
  928.             
  929.             if ((errCode = GetEOF(fRefNum,&byteCount))!=noErr)
  930.             {
  931.                 FSClose(fRefNum);
  932.                 P2CStr((unsigned char *) mySFReply.fName);
  933.                 sprintf(tempString,"InsertTextFile: GetEOF('%s',0) Error %d",mySFReply.fName,errCode);
  934.                 ErrorAlert(tempString);
  935.                 return(true);
  936.             }
  937.             
  938.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  939.             {
  940.                 FSClose(fRefNum);
  941.                 P2CStr((unsigned char *) mySFReply.fName);
  942.                 sprintf(tempString,"InsertTextFile: SetFPos('%s',0) Error %d",mySFReply.fName,errCode);
  943.                 ErrorAlert(tempString);
  944.                 return(true);
  945.             }
  946.             
  947.             theText = NewPtr(byteCount);
  948.             
  949.             if (MemError())
  950.             {
  951.                 FSClose(fRefNum);
  952.                 sprintf(tempString,"InsertTextFile: NewPtr(%ld) Error %d",byteCount,MemError());
  953.                 ErrorAlert(tempString);
  954.                 return(true);
  955.             }
  956.             
  957.             if ((errCode = FSRead(fRefNum,&byteCount,theText))!=noErr)
  958.             {
  959.                 DisposePtr(theText);
  960.                 FSClose(fRefNum);
  961.                 P2CStr((unsigned char *) mySFReply.fName);
  962.                 sprintf(tempString,"InsertTextFile: FSRead('%s') Error %d",mySFReply.fName,errCode);
  963.                 ErrorAlert(tempString);
  964.                 return(true);
  965.             }
  966.             
  967.             if ((errCode = FSClose(fRefNum))!=noErr)
  968.             {
  969.                 DisposePtr(theText);
  970.                 P2CStr((unsigned char *) mySFReply.fName);
  971.                 sprintf(tempString,"InsertTextFile: FSClose('%s',0) Error %d",mySFReply.fName,errCode);
  972.                 ErrorAlert(tempString);
  973.                 return(true);
  974.             }
  975.             
  976.             SetCursor(&waitCursor);
  977.             
  978.             TE32KInsert(theText,byteCount,theTEH);
  979.             
  980.             InitCursor();
  981.             
  982.             DisposePtr(theText);
  983.         }
  984.         
  985.         return(true);
  986.     }
  987.     
  988.     else
  989.         return(false);
  990. }
  991.  
  992.  
  993.  
  994.  
  995. DoFontSize(theFontName,theFontSize)
  996. char *theFontName;short    theFontSize;
  997. {
  998. short            theFontNum;
  999. Rect        tempRect;
  1000. GrafPtr        oldPort;
  1001.     
  1002.     if (theTEH32K)
  1003.     {
  1004.         if (theFontName[0])
  1005.         {
  1006.             C2PStr(theFontName);
  1007.             GetFNum((ConstStr255Param) theFontName,&theFontNum);
  1008.         }
  1009.         else
  1010.             theFontNum = -1;
  1011.         
  1012.         
  1013.         if (theFontNum < 0)
  1014.             theFontNum = defaultFont;
  1015.         
  1016.         if (theFontSize <= 0)
  1017.             theFontSize = defaultFontSize;
  1018.             
  1019.         defaultFont = theFontNum;
  1020.         defaultFontSize = theFontSize;
  1021.         
  1022.         TE32KSetFontStuff(theFontNum,0,srcCopy,theFontSize,theTEH32K);
  1023.         
  1024.         AdjustForResizedWindow();
  1025.         
  1026.         GetPort(&oldPort);
  1027.         SetPort((**theTEH32K).inPort);
  1028.         tempRect = (*(**theTEH32K).inPort).portRect;
  1029.         InvalRect(&tempRect);
  1030.         SetPort(oldPort);
  1031.         
  1032.         MashortainFontMenu();
  1033.     }
  1034. }
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040. MashortainFontMenu()
  1041. {
  1042. short        i,numItems,tempSize;
  1043. char    theFontString[128];
  1044. char    tempString[256];
  1045.  
  1046.     numItems = CountMItems(myMenus[fontMenu]);
  1047.     
  1048.     GetFontName(defaultFont,(unsigned char *) theFontString);
  1049.     P2CStr((unsigned char *)theFontString);
  1050.     
  1051.     for (i=1;i<=numItems-7;i++)
  1052.     {
  1053.         GetItem(myMenus[fontMenu],i,(unsigned char *)tempString);
  1054.         P2CStr((unsigned char *)tempString);
  1055.         
  1056.         if (strcmp(tempString,theFontString) == EQSTR)
  1057.         {
  1058.             CheckItem(myMenus[fontMenu],i,true);
  1059.             SetItemMark(myMenus[fontMenu],i,checkMark);
  1060.         }
  1061.         else
  1062.             CheckItem(myMenus[fontMenu],i,false);
  1063.     }
  1064.     
  1065.     
  1066.     sprintf(theFontString,"%d",defaultFontSize);
  1067.     
  1068.     for (i=numItems-6;i<=numItems;i++)
  1069.     {
  1070.         GetItem(myMenus[fontMenu],i,(unsigned char *)tempString);
  1071.         P2CStr((unsigned char *)tempString);
  1072.         
  1073.         if (sscanf(tempString,"%d",&tempSize) != 1)
  1074.             tempSize = -1;
  1075.         
  1076.         if (strcmp(tempString,theFontString) == EQSTR)
  1077.         {
  1078.             CheckItem(myMenus[fontMenu],i,true);
  1079.             SetItemMark(myMenus[fontMenu],i,checkMark);
  1080.         }
  1081.         else
  1082.             CheckItem(myMenus[fontMenu],i,false);
  1083.         
  1084.         if (RealFont(defaultFont,tempSize))
  1085.             SetItemStyle(myMenus[fontMenu],i,outline);
  1086.         else
  1087.             SetItemStyle(myMenus[fontMenu],i,0);
  1088.     }
  1089. }
  1090.  
  1091.  
  1092.  
  1093.  
  1094. CreateBigFile()
  1095. {
  1096. short            errCode,fRefNum;
  1097. long        byteCount,i;
  1098. char        tempString[256];
  1099. SFReply        mySFReply;
  1100. Point    theTopLeft;
  1101.  
  1102.     theTopLeft.h = qd.screenBits.bounds.left + (qd.screenBits.bounds.right - qd.screenBits.bounds.left - 304)/2;;
  1103.     theTopLeft.v = qd.screenBits.bounds.top + 50;
  1104.     
  1105.     InitCursor();
  1106.     
  1107.     strcpy((char *) mySFReply.fName,"Big Text File");
  1108.     C2PStr((char *) mySFReply.fName);
  1109.     
  1110.     SFPutFile(theTopLeft,"\pSave 10,000 line file as:",mySFReply.fName,0L,&mySFReply);
  1111.     
  1112.     if (!(mySFReply.good))
  1113.         return;
  1114.     
  1115.     if ((errCode = FSOpen(mySFReply.fName,mySFReply.vRefNum,&fRefNum))!=noErr)
  1116.     {
  1117.         if (errCode==fnfErr)
  1118.         {
  1119.             if ((errCode = Create(mySFReply.fName,mySFReply.vRefNum,'    ',
  1120.                                     'TEXT')) != noErr)
  1121.             {
  1122.                 P2CStr((unsigned char *) mySFReply.fName);
  1123.                 sprintf(tempString,"createBigFile: Create('%s') Error %d",(char *) mySFReply.fName,errCode);
  1124.                 ErrorAlert(tempString);
  1125.                 return(false);
  1126.             }
  1127.             
  1128.             errCode = FSOpen(mySFReply.fName,mySFReply.vRefNum,&fRefNum);
  1129.         }
  1130.         
  1131.         if (errCode != noErr)
  1132.         {
  1133.             P2CStr((unsigned char *) mySFReply.fName);
  1134.             sprintf(tempString,"createBigFile: FSOpen('%s') Error %d",(char *) mySFReply.fName,errCode);
  1135.             ErrorAlert(tempString);
  1136.             return(false);
  1137.         }
  1138.     }
  1139.             
  1140.     if ((errCode = SetEOF(fRefNum,0L))!=noErr)
  1141.     {
  1142.         FSClose(fRefNum);
  1143.         P2CStr((unsigned char *) mySFReply.fName);
  1144.         sprintf(tempString,"createBigFile: SetEOF('%s',0L) Error %d",(char *) mySFReply.fName,errCode);
  1145.         ErrorAlert(tempString);
  1146.         return(false);
  1147.     }
  1148.     
  1149.     if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  1150.     {
  1151.         FSClose(fRefNum);
  1152.         P2CStr((unsigned char *) mySFReply.fName);
  1153.         sprintf(tempString,"createBigFile: SetFPos('%s',0) Error %d",(char *) mySFReply.fName,errCode);
  1154.         ErrorAlert(tempString);
  1155.         return(false);
  1156.     }
  1157.     
  1158.     SetCursor(&waitCursor);
  1159.     
  1160.     for (i=0;i<10000;i++)
  1161.     {
  1162.         sprintf(tempString,"line %ld\r",i);
  1163.         byteCount = strlen(tempString);
  1164.         
  1165.         if ((errCode = FSWrite(fRefNum,&byteCount,tempString))!=noErr)
  1166.         {
  1167.             InitCursor();
  1168.             FSClose(fRefNum);
  1169.             P2CStr((unsigned char *) mySFReply.fName);
  1170.             sprintf(tempString,"createBigFile: FSWrite('%s') Error %d",(char *) mySFReply.fName,errCode);
  1171.             ErrorAlert(tempString);
  1172.             return(false);
  1173.         }
  1174.     }
  1175.         
  1176.     if ((errCode = FSClose(fRefNum))!=noErr)
  1177.     {
  1178.         InitCursor();
  1179.         P2CStr((unsigned char *) mySFReply.fName);
  1180.         sprintf(tempString,"createBigFile: FSClose('%s') Error %d",(char *) mySFReply.fName,errCode);
  1181.         ErrorAlert(tempString);
  1182.         return(false);
  1183.     }
  1184.     
  1185.     FlushVol("\p",mySFReply.vRefNum);
  1186.     
  1187.     InitCursor();
  1188.     
  1189.     return(true);
  1190. }
  1191.